<?php

    function UnitTableInsert( tStringArray $values, tCoalaPointer $row, tInteger $startrow, tInteger $numrows, tInteger $curpage ) {
        global $table;
        
        $startrow = $startrow->Get();
        $numrows = $numrows->Get();
        $curpage = $curpage->Get();
        
        $values = $values->Get();
        $fieldNames = array_keys( $table->Fields );
        $inserts = array();
        for ( $i = 0; $i < count( $values ); ++$i ) {
            $inserts[ $fieldNames[ $i ] ] = $values[ $i ];
        }

        $change = $table->InsertInto( $inserts );

        if ( !$change->Impact() ) {
            ?>alert( 'Inserting row failed!' );<?php
        }

        ++$numrows;

        if ( $numrows - $startrow + 1 > PAGE_LIMIT ) {
            $curpage = ceil( $table->NumRows / PAGE_LIMIT ) - 1; // PAGE_LIMIT: libs/baboo
        }

        ?>var tablebrowse = g( 'tablebrowse' );<?php

        ob_start();
        Element( 'table/browse/view', $curpage );
        $html = ob_get_clean();
        
        ?>var newTableBrowse = d.createElement( 'div' );
        newTableBrowse.id = 'tablebrowse';
        newTableBrowse.innerHTML = <?php
        echo w_json_encode( $html );
        ?>;
        
        tablebrowse.parentNode.insertBefore( newTableBrowse, tablebrowse );
        tablebrowse.parentNode.removeChild( tablebrowse );<?php
    }

?>
